<?php
//echo $html->css('/deals/css/deals.css');
if(!isset($showDeleteButton))
{
	$showDeleteButton = false;
}
$currencySymbol = cr("DealConfig.currency_symbol");
foreach ($deals as $deal)
{
	$dealUrl = $html->url(array("plugin"=>"deals","controller"=>"deals","action"=>"view",$deal['Deal']['id']), true);

	$dealTitle = str_replace("&nbsp;"," ",$deal['Deal']['title']);
	$dealTitle = strip_tags($dealTitle);
	if(strlen($dealTitle) > 145)
	{
		$dealTitleArray = explode(" ", $dealTitle);
		$dealTitle = "";
		foreach($dealTitleArray as $descText)
		{
			$dealTitle .= $descText;
			if(strlen($dealTitle) > 145)
			{
				break;
			}
			$dealTitle .= " ";
		}
	}
		
	$dealDesc = str_replace("&nbsp;"," ",$deal['Deal']['description']);
	$dealDesc = strip_tags($dealDesc);
	$showSeeMoreLink = false;
	if(strlen($dealDesc) > 300)
	{
		$dealDescArray = explode(" ", $dealDesc);
		$dealDesc = "";
		foreach($dealDescArray as $descText)
		{
			$dealDesc .= $descText;
			if(strlen($dealDesc) > 300)
			{
				break;
			}
			$dealDesc .= " ";
		}
		$showSeeMoreLink = true;
	}
	?>
	<div class="listing_deals">
		<div class="line deal_summary clickable"> 
			<div class="deal_body"> 
				<div class="fl col-xs-6 col-md-5 image">
					<?php echo $html->link($html->image($deal['Deal']['image'], array('alt'=>$deal['Deal']['title'],'thumb'=>cr('Thumbs.listingSmall'), 'height'=>'130', 'width'=>'225')), $dealUrl, array('escape'=>false)); ?>
					<div class="offer_ribbon"><?php echo __("Deal"); ?></div>  
				</div>
				<div class="fl col-xs-12 col-md-7 deal_info_review">
					<a class="deal_title" href="<?php echo $dealUrl; ?>"><span><?php echo $dealTitle; ?></span></a>
					<?php echo $dealDesc; ?>
					<?php if($showSeeMoreLink) { ?>
						<a href='<?php echo $dealUrl; ?>'><?php echo __("...See More");?></a> 
					<?php } ?> 
				</div>
				<?php 
				$showDeleteHtml = "";
				if ($showDeleteButton == true && $deal["Deal"]["user_id"] == @$session->read("Auth.User.id")) 
				{
					ob_start();
				?>
					<div class="delete_deal_link">
						<?php echo $html->link(__("Delete"),"javascript:void(0);",array('class'=>'button','onclick'=>'return deleteDeal(this,"'.$deal["Deal"]["id"].'");')); ?>
					</div>
				<?php 
					$showDeleteHtml = ob_get_clean();
				}
				?>
				<div class="deal_payment">
					<?php echo $showDeleteHtml; ?>
					<div class="line">
						<div class="unit size3of5">
							<div class="line pricing_deal">
								<div class="unit size1of3"><?php echo __("Reg. Price"); ?></div>
								<div class="unit size1of3"><?php echo __("Discount"); ?></div>
								<div class="unit size1of3"><?php echo __("Savings"); ?></div>
							</div>
							<?php 
							$discount = $deal['Deal']['discount']; //considering discount is entered in perc
							$saving = $discount; //considering discount is entered as flat amt
				
							if($deal['Deal']['discount_type']==0) //if it is flat
							{//get discount perc
								$discount = $deal['Deal']['discount'] / $deal['Deal']['regular_price']*100;
							}
							else
							{//get savings amount
								$saving = $deal['Deal']['regular_price'] - $deal['Deal']['finalPrice'];
							}
							?>
							<div class="line price_deal">
								<div class="unit size1of3"><?php echo $currencySymbol."".number_format($deal['Deal']['regular_price'],2,'.',''); ?></div>
								<div class="unit size1of3"><?php echo number_format($discount,2,'.','')."%"; ?></div>
								<div class="unit size1of3"><?php echo $currencySymbol."".number_format($saving,2,'.',''); ?></div>
							</div>
						</div>
						<div class="unit size2of5"><?php 
						$differenceInSeconds = (strtotime($deal['Deal']['expires'])+(24*60*60)-1) - time();
						$showBuyButton = 1;
						if($differenceInSeconds<1 || (($deal['Deal']['total_quantity']!=0) && ($deal['Deal']['total_quantity']<=$deal['Deal']['quantity_consumed']))) // || $deal['Deal']['purchase_link_status']==Deal::PURCHASE_LINK_OFF
						{
							$showBuyButton = 0;
						}
						if($showBuyButton)
						{
							echo $html->link($currencySymbol."".$deal['Deal']['finalPrice']." ".__("Buy Now"),array("plugin"=>"deals","controller"=>"deal_orders","action"=>"add",$deal['Deal']['id']),array('class'=>'button'));
						}
						?>
						</div>
					</div>
				</div> <!--deal_payment-->
			</div><!--deal_body-->
		</div><!--deal_summary-->                                       
	</div><!--listing_deals-->
<?php
}
?>
<?php if ($showDeleteButton == true) { ?>
<script>
	function deleteDeal(callerObj, dealId)
	{
		var delUrl = '<?php echo $html->url(array("plugin"=>"deals", "controller"=>"deals", "action"=>"delete")) ?>';
		delUrl = delUrl + '/' + dealId;
		if(confirm('<?php echo __("Are you sure ?")?>'))
		{
			window.location = delUrl;
		}
		return false;
	}
</script>
<?php } ?>